/**
 * file
 */
package com.tdkj.action.front.member.seller.shop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanType;
import com.tdkj.model.shop.BeanGood;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.shop.IBeanGoodService;
import com.tdkj.util.PublicConfing;

public class GoodInnerTypeAction extends BaseAction {
	
	private Long innerTypeId;
	
	private List<Map<String,String>> isNullGoods = new ArrayList<Map<String,String>>();
	
	private List<Map<String,String>> selectedGoods = new ArrayList<Map<String,String>>();
	
	private IBeanGoodService goodService;
	
	private Long[] selectedIds;
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() {
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		BeanType type = goodService.findById(innerTypeId, BeanType.class);
		
		//内部未归类，已上架
		List<BeanGood> isNullGoodsTemp = goodService.getBaseDao().getSession().createCriteria(BeanGood.class)
		.add(Restrictions.isNull("innerType"))
		.add(Restrictions.isNotNull("shelf"))
		.add(Restrictions.eq("user", user))
		.list();
		
		for(BeanGood good : isNullGoodsTemp){
			Map<String,String> goodMap = new HashMap<String,String>();
			goodMap.put("id", String.valueOf(good.getId()));
			goodMap.put("name", String.valueOf(good.getName()));
			
			isNullGoods.add(goodMap);
		}
		
		List<BeanGood> selectedGoodsTemp = goodService.getBaseDao().getSession().createCriteria(BeanGood.class)
		.add(Restrictions.eq("innerType", type))
		.add(Restrictions.isNotNull("shelf"))
		.add(Restrictions.eq("user", user))
		.list();
		
		for(BeanGood good : selectedGoodsTemp){
			
			Map<String,String> goodMap = new HashMap<String,String>();
			goodMap.put("id", String.valueOf(good.getId()));
			goodMap.put("name", String.valueOf(good.getName()));
			
			selectedGoods.add(goodMap);
		}
		return "success";
	}
	
	public String update() {
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		
		goodService.updateInnerTypeSelected(innerTypeId, user.getId(), selectedIds);
		return "success";
	}

	/**
	 * @return the innerTypeId
	 */
	public Long getInnerTypeId() {
		return innerTypeId;
	}

	/**
	 * @param innerTypeId the innerTypeId to set
	 */
	public void setInnerTypeId(Long innerTypeId) {
		this.innerTypeId = innerTypeId;
	}

	/**
	 * @return the isNullGoods
	 */
	public List<Map<String,String>> getIsNullGoods() {
		return isNullGoods;
	}

	/**
	 * @param isNullGoods the isNullGoods to set
	 */
	public void setIsNullGoods(List<Map<String,String>> isNullGoods) {
		this.isNullGoods = isNullGoods;
	}

	/**
	 * @return the selectedGoods
	 */
	public List<Map<String,String>> getSelectedGoods() {
		return selectedGoods;
	}

	/**
	 * @param selectedGoods the selectedGoods to set
	 */
	public void setSelectedGoods(List<Map<String,String>> selectedGoods) {
		this.selectedGoods = selectedGoods;
	}

	/**
	 * @return the selectedIds
	 */
	public Long[] getSelectedIds() {
		return selectedIds;
	}

	/**
	 * @param selectedIds the selectedIds to set
	 */
	public void setSelectedIds(Long[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	/**
	 * @param goodService the goodService to set
	 */
	@Resource
	public void setGoodService(IBeanGoodService goodService) {
		this.goodService = goodService;
	}
	
	
}
